from main.task.models import Task
from main.task.serializers import TaskSerializer, TaskCreateUpdateSerializer
from utils.viewset import CustomModelViewSet
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from libs import json_response


class TaskModelViewSet(CustomModelViewSet):
    """
    排期任务 的CRUD视图
    """
    queryset = Task.objects.all()
    serializer_class = TaskSerializer
    create_serializer_class = TaskCreateUpdateSerializer
    update_serializer_class = TaskCreateUpdateSerializer


@api_view(['GET'])
@permission_classes((IsAuthenticated,))
def get_owner_list(request):
    tasks = Task.objects.filter()
    owner = [x['owner'] for x in tasks.order_by('owner').values('owner').distinct()]
    return json_response(data=owner)


@api_view(['GET'])
@permission_classes((IsAuthenticated,))
def get(self, request):
    undo = Task.objects.filter(delete=False, status=0).count()
    do = Task.objects.filter(delete=False, status=1).count()
    return json_response(data={'undo': undo, 'do': do})

# # 查询排期任务模板
# @api_view(['GET'])
# @permission_classes((IsAuthenticated,))
# def get_schedule_tmp(request):
#     params, error = JsonParser(
#         Argument('channel_id', help='请选择渠道'),
#         Argument('type', help='请选择排期类型')
#     ).parse(request.GET)
#     if error:
#         return json_response(message=error)
#     params.template = 1
#     task = Task.objects.filter(**params).order_by('-date_time').first()
#     if not task:
#         return json_response(message='当前渠道没有创建模板')
#     return json_response(data=task.to_dict(excludes='id'))
#
#
# # 查询渠道版本
# @api_view(['GET'])
# @permission_classes((IsAuthenticated,))
# def get_version(request):
#     params, error = JsonParser(
#         Argument('channel_id', help='请选择渠道'),
#     ).parse(request.GET)
#     pass
#     if error:
#         return json_response(message=error)
#     channel = Channel.objects.filter(id=params.channel_id).first()
#     versions = GameVersion.objects.filter(game_type=channel.project.name_en, language=channel.language).order_by(
#         '-created_at')[
#                :5]
#     version_list = []
#     for ver in versions:
#         version_list.append(ver.version + '-' + ver.language)
#     return json_response(data=version_list)
